package com.zhongketech.sunflower.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.provider.MediaStore;
import android.provider.MediaStore.Video.Thumbnails;
import android.support.v4.view.ViewPager;
import android.text.ClipboardManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhongketech.sunflower.Constant;
import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.adapter.ExpressionAdapter;
import com.zhongketech.sunflower.adapter.ExpressionPagerAdapter;
import com.zhongketech.sunflower.adapter.MessageAdapter;
import com.zhongketech.sunflower.adapter.VoicePlayClickListener;
import com.zhongketech.sunflower.control.PhoneManager;
import com.zhongketech.sunflower.sv.SvManager;
import com.zhongketech.sunflower.transport.LogUtil;
import com.zhongketech.sunflower.utils.CommonUtils;
import com.zhongketech.sunflower.utils.ImageUtil;
import com.zhongketech.sunflower.utils.SmileUtils;




/**
 * 控制页面
 * 
 */
public class ControlActivity extends BaseActivity {


	private static final String TAG = "ControlActivity";

	private ListView listView ;
	private Context context;


	private List<AdaterData> myData = new ArrayList<AdaterData>();


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.control_activity);
		context = this;
		initData();
		initView();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(KeyEvent.KEYCODE_BACK == keyCode)
		{
			back();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	private void initData()
	{
		AdaterData ad1 = new AdaterData();
		ad1.imageViewId = R.drawable.control_phone;
		ad1.itemText = "通话设置";
		if(SvManager.getInstance().isPhoneEnable())
		{
			ad1.statusText = "已开启";	
		}
		else
		{
			ad1.statusText = "未开启";
		}
		ad1.textview2 = null;
		myData.add(ad1);


		AdaterData ad2 = new AdaterData();
		ad2.imageViewId = R.drawable.control_sms;
		ad2.itemText = "短信设置";
		if(SvManager.getInstance().isSmsEnable())
		{
			ad2.statusText = "已开启";	
		}
		else
		{
			ad2.statusText = "未开启";
		}
		ad2.textview2 = null;
		myData.add(ad2);


		AdaterData ad3 = new AdaterData();
		ad3.imageViewId = R.drawable.control_lock;
		ad3.itemText = "锁定屏幕";
		if(SvManager.getInstance().isLock())
		{
			ad3.statusText = "已锁屏";	
		}
		else
		{
			ad3.statusText = "未锁屏";
		}
		ad3.textview2 = null;
		myData.add(ad3);

		AdaterData ad4 = new AdaterData();
		ad4.imageViewId = R.drawable.control_net;
		ad4.itemText = "断开网络";
		if(SvManager.getInstance().isWebEnable())
		{
			ad4.statusText = "已断网";	
		}
		else
		{
			ad4.statusText = "未断网";
		}

		ad4.textview2 = null;
		myData.add(ad4);

		AdaterData ad5 = new AdaterData();
		ad5.imageViewId = R.drawable.control_eyes;
		ad5.itemText = "眼睛保护";
		ad5.statusText = null;
		String[] time = SvManager.getInstance().eyesTime();
		if(null == time)
		{
			ad5.textview2 = "未启用";	
		}
		else
		{
			ad5.textview2="可连续使用"+time[0]+"分钟,"+"需休息"+time[1]+"分钟";
		}
		
		myData.add(ad5);

		AdaterData ad6 = new AdaterData();
		ad6.imageViewId = R.drawable.control_app;
		ad6.itemText = "应用管理";
		ad6.statusText = "";
		if(SvManager.getInstance().appEnable().equals(Constant.APP_ENABLE_ALL))
		{
			ad6.textview2 = "全部可用";	
		}
		else if(SvManager.getInstance().appEnable().equals(Constant.APP_LIST))
		{
			ad6.textview2 = "部分禁止";
		}
		else
		{
			ad6.textview2 = "全部禁止";
		}

		myData.add(ad6);
	}

	private void initView()
	{
		listView = (ListView) findViewById(R.id.listView);
		listView.setAdapter(new MyAdapter());
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//				LogUtil.i("ZK","position:"+position+" id:"+id);
				switch (position) {
				case 0:

					break;
				case 1:
					
					break;
				case 2:

					break;
				case 3:

					break;
				case 4:

					break;
				case 5:
					if(Constant.APP_DISABLE_ENABLE.equals(SvManager.getInstance().appEnable()))
					{
						MyToast.getInstance().show("全部禁止");
					}
					else if(Constant.APP_ENABLE_ALL.equals(SvManager.getInstance().appEnable()))
					{
						MyToast.getInstance().show("全部启用");
						//						startActivity(new Intent(context.getApplicationContext(),AppActivity.class));
					}
					else
					{
						startActivity(new Intent(context.getApplicationContext(),AppActivity.class));
					}
					break;

				default:
					break;
				}
			}
		});
	}


	public final class ViewHolder {
		ImageView imageView;
		LinearLayout itemlayout;
		TextView textView1;
		TextView textView2;
		LinearLayout statusLayout;
		ImageView statusImage;
		TextView statusText;
	}



	public final class AdaterData{
		int imageViewId;
		String itemText;
		String textview2;
		String statusText; //为空则所有隐藏，为""则显示图标，反之：显示具体文字
	}

	public class MyAdapter extends BaseAdapter
	{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return myData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			ViewHolder holder = null;
			if(null == convertView)
			{
				holder = new ViewHolder(); 
				convertView =  LayoutInflater.from(context).inflate(R.layout.control_item, null);
				holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
				holder.itemlayout = (LinearLayout) convertView.findViewById(R.id.itemlayout);
				holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
				holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
				holder.statusLayout = (LinearLayout) convertView.findViewById(R.id.statusLayout);
				holder.statusImage = (ImageView) convertView.findViewById(R.id.statusImage);
				holder.statusText = (TextView) convertView.findViewById(R.id.statusText);
				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder)convertView.getTag();
			}

			holder.imageView.setBackgroundResource(myData.get(position).imageViewId);
			//holder.itemlayout = (LinearLayout) convertView.findViewById(R.id.itemlayout);
			holder.textView1.setText(myData.get(position).itemText);

			if(null != myData.get(position).textview2)
			{
				holder.textView2.setText(myData.get(position).textview2);
				holder.textView2.setVisibility(View.VISIBLE);
			}
			else
			{
				holder.textView2.setVisibility(View.GONE);
			}

			if(null != myData.get(position).statusText)
			{
				if(!("".equals(myData.get(position).statusText)))
				{
					holder.statusText.setText(myData.get(position).statusText);
					holder.statusText.setVisibility(View.VISIBLE);
					holder.statusImage.setVisibility(View.GONE);
				}
				else
				{
					holder.statusText.setVisibility(View.GONE);
					holder.statusImage.setVisibility(View.VISIBLE);
				}

			}
			else
			{
				holder.statusText.setVisibility(View.GONE);
				holder.statusImage.setVisibility(View.GONE);
			}

			return convertView;
		}

	}

	private void back()
	{
		finish();
	}

}
